<!DOCTYPE html>

<html lang="en">
    <head>
        <meta charset="UTF-8"/>
        <title>curry</title>
        <style scoped>
            body {
                width: 100%;
                height: 100%;
                margin: 0;
                background-color: bisque;
            }
        </style>
    </head>

    <body>
        <div class="box" id="box">curry box</div>
    </body>

    <script>
        function sum(a, b, c) {
            return a + b + c
        }

        function curry(func) {
            return function curried(...args) {
                if (args.length >= func.length) {
                    return func.apply(this, args);
                } else {
                    return function(...args2) {
                        return curried.apply(this, args.concat(args2));
                    }
                }
            };

        }

        const currySum = curry(sum)
        console.log('-12-', currySum(1,2,3))
        console.log('-14-', currySum(1)(2,3))
        console.log('-13-', currySum(1)(2)(3))
        // 处理错误
        console.log('-15-', currySum(1)(2))
        console.log('-11-', currySum(1))

        function currying(fn) {
           return function(a) {
            return function(b) {
                return function(c) {
                    return fn(a,b,c)
                }
            }
           }
        }

        const curryingSum = currying(sum)
        console.log('-21-', curryingSum(1))
        console.log('-22-', curryingSum(1,2,3))
        console.log('-25-', curryingSum(1)(2)(3))

    </script>
</html>